﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Driverplugin;
using Grpc.Core;
using IoTEdge.Grpc.Server.BaseLibs.entities;
using NLog;
using static Driverplugin.DeviceStatus.Types;

namespace IoTEdge.Grpc.Server.BaseLibs.services
{
    public abstract class DeviceCollectBase
    {
        private ILogger logger = LogManager.GetCurrentClassLogger();
        private Metadata metadata = null;
        public Dictionary<string, DeviceModelBase> Instance = new();
        AsyncDuplexStreamingCall<Response, DeviceLifeControlRequest>? lifeControl = null;
        DriverPlugin.DriverPluginClient client = null;
        CancellationTokenSource _TokenSource = new CancellationTokenSource();

        public virtual void Start(CancellationToken token)
        {

        }

        public virtual void MainInitInstance()
        {

        }
        public virtual async Task GetAccessToken()
        {
            await Task.CompletedTask;
        }
        public virtual void GetServerConfig()
        {

        }
        public virtual async void handleLifeControl()
        {
            await Task.CompletedTask;
        }
        public virtual void StartDevice(DeviceMeta device, ref Response response)
        {

        }
        public virtual void StopDevice(DeviceMeta device, ref Response response)
        {

        }
        public virtual void SendMessageAction(DeviceModelBase device, Driverplugin.Data data)
        {

        }
        public virtual void SendStatusAction(DeviceModelBase dev, DeviceState state, string msg)
        {

        }
        public int ConvertIntervalMS(string input)
        {
            int value = 1000;

            if (input.EndsWith("ms"))
            {
                input = input.Remove(input.Length - 2, 1);
                value = int.Parse(input);
            }
            else if (input.EndsWith("s"))
            {
                input = input.Remove(input.Length - 1, 1);
                value *= int.Parse(input);
            }
            else if (input.EndsWith("m"))
            {
                input = input.Remove(input.Length - 2, 1);
                value *= int.Parse(input);
                value *= 60;
            }
            else if (input.EndsWith("h"))
            {
                input = input.Remove(input.Length - 2, 1);
                value *= int.Parse(input);
                value *= 60 * 60;
            }
            else
            {
                value *= 30;
            }

            return value;
        }
    }
}
